/**
 * @author 李亚松
 * @email  lysong01@gmail.com
 * 
 * SinaBlogCrawler.java 2012-12-2
 * description 
 */
package liyasong.tool.crawler;

import liyasong.util.RegexUtil;

import org.htmlparser.NodeFilter;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.NotFilter;
import org.htmlparser.filters.TagNameFilter;

public class SinaBlogCrawler extends AbstractCrawler {

	public SinaBlogCrawler(String blogSite) {
		super(blogSite);
	}

	/**
	 * @param args	只输入一个sina博客地址即可
	 * TODO 这里最好将参数格式弄成 -s xxx -o xxx
	 */
	public static void main(String[] args) {
		if (args.length != 2) {
			System.out.println("输入的参数数目不正确");
		} else {
			String blogSite = args[0];	//TODO 这里需不需要对网址进行检测呢?
			Crawler crawler = new SinaBlogCrawler(blogSite);
			crawler.crawl();
			String outPath	= args[1];
			crawler.resultWrite(outPath);
			System.out.println("the number of blogs:"+crawler.getArticles().size());
		}
	}

	@Override
	protected NodeFilter firstLinkFilter() {
		NodeFilter parent = new AndFilter(new TagNameFilter("div"), 
				new HasAttributeFilter("class", "blog_title"));
		NodeFilter nodeFilter = new AndFilter(new TagNameFilter("a"), 
				new HasParentFilter(parent));
		return nodeFilter;
	}

	@Override
	protected NodeFilter lastLinkFilter() {
		NodeFilter parent = new AndFilter(new TagNameFilter("div"), 
				new HasAttributeFilter("class", "SG_floatL"));
		NodeFilter nodeFilter = new AndFilter(new TagNameFilter("a"), 
				new HasParentFilter(parent));
		return nodeFilter;
	}

	@Override
	protected NodeFilter titleFilter() {
		NodeFilter nodeFilter = new AndFilter(new TagNameFilter("h2"), 
				new HasAttributeFilter("class", "titName SG_txta"));
		return nodeFilter;
	}

	@Override
	protected NodeFilter authorFilter() {
		NodeFilter nodeFilter = new AndFilter(new TagNameFilter("span"), 
				new HasAttributeFilter("id", "blognamespan"));
		return nodeFilter;
	}

	@Override
	protected NodeFilter descriptionFilter() {
		NodeFilter displayNone = new NotFilter(new HasAttributeFilter("class", "MASS82f2ae03d5b9"));
		NodeFilter nodeFilter = new AndFilter(displayNone, 
				new HasAttributeFilter("class", "articalContent  "));
		return nodeFilter;
	}

	@Override
	protected NodeFilter pubDateFilter() {
		NodeFilter nodeFilter = new AndFilter(new TagNameFilter("span"), 
				new HasAttributeFilter("class", "time SG_txtc"));
		return nodeFilter;
	}

	@Override
	protected String cleanDescription(String description) {
		return RegexUtil.cleanSinaBlog(description);
//		return RegexUtil.replaceBlankLine(description);
	}

	@Override
	protected String cleanTitle(String title) {
		return RegexUtil.replaceSpace(title);
	}

	@Override
	protected String cleanAuthor(String author) {
		return RegexUtil.cleanSpace(author);
	}

}
